﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;


namespace Gene_Explorer_Client
{
    public partial class GeneExplorerClient : Form
    {
     //   public bool flag = false;
     //   private int splitterDistance;
     //   private Point buttonHide;
        private string path = Application.StartupPath + "//实验目录";
        //缩略图
        private Pen boundPen = new Pen(Color.Gainsboro);
        private Pen selPen = new Pen(Color.Blue, 2);                //画选中图的边框
        private SolidBrush textBrush = new SolidBrush(Color.Black);
        private SolidBrush bgBrush;
        private StringFormat format = new StringFormat(StringFormatFlags.NoClip);
     //   StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);

        //查看图片
        private Bitmap bmpInPb;
        
        private int bmpIndex;

        public List<Lab> labList = new List<Lab>(100);

        public GeneExplorerClient()
        {
            InitializeComponent();
      //      this.skinEngine1.SkinFile = "DiamondBlue.ssk";
        }

        private void GeneExplorer_Load(object sender, EventArgs e)
        {
            //    splitContainer1.SplitterWidth = 2;

            //Demo演示代码
            treeView_Result.Nodes.Add("kmeans");
            treeView_Result.Nodes[0].Nodes.Add("kmeans-1");
            treeView_Result.Nodes[0].Nodes.Add("kmeans-2");
            treeView_Result.Nodes[0].Nodes.Add("kmeans-3");
            treeView_Result.Nodes[0].Nodes.Add("kmeans-4");
            
            treeView_Result.Nodes.Add("FCM");
            treeView_Result.Nodes[1].Nodes.Add("FCM-1");
            treeView_Result.Nodes[1].Nodes.Add("FCM-2");
            treeView_Result.Nodes[1].Nodes.Add("FCM-3");
            treeView_Result.Nodes[1].Nodes.Add("FCM-4");

            treeView_Result.Nodes[1].Nodes[2].Nodes.Add("FCM-3-1");
            treeView_Result.Nodes[1].Nodes[2].Nodes.Add("FCM-3-2");
            treeView_Result.Nodes[1].Nodes[2].Nodes.Add("FCM-3-3");

            treeView_Result.Nodes.Add("SOM");
            treeView_Result.Nodes[2].Nodes.Add("SOM-1");
            treeView_Result.Nodes[2].Nodes.Add("SOM-2");
            treeView_Result.Nodes[2].Nodes.Add("SOM-3");
            treeView_Result.Nodes[2].Nodes.Add("SOM-4");
            treeView_Result.Nodes[2].Nodes.Add("SOM-5");
            treeView_Result.Nodes[2].Nodes.Add("SOM-6");

            treeView_Result.Nodes[2].Nodes[0].Nodes.Add("SOM-1-1");
            treeView_Result.Nodes[2].Nodes[0].Nodes.Add("SOM-1-2");
            treeView_Result.Nodes[2].Nodes[0].Nodes.Add("SOM-1-3");
            treeView_Result.Nodes[2].Nodes[0].Nodes.Add("SOM-1-4");

            treeView_Result.Nodes[2].Nodes[3].Nodes.Add("SOM-4-kmeans-1");
            treeView_Result.Nodes[2].Nodes[3].Nodes.Add("SOM-4-kmeans-2");
            treeView_Result.Nodes[2].Nodes[3].Nodes.Add("SOM-4-kmeans-3");
            treeView_Result.Nodes[2].Nodes[3].Nodes.Add("SOM-4-kmeans-4");
            treeView_Result.ExpandAll();
            //Demo演示代码

            bgBrush = new SolidBrush(listView_Result.BackColor);
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            DirectoryInfo dir = new DirectoryInfo(path);
            foreach (DirectoryInfo d in dir.GetDirectories())
            {
                LabFolder labFolder = new LabFolder(Application.StartupPath, d.Name);
                listBox_Lab.Items.Add(labFolder);
            }

            if (listBox_Lab.Items.Count > 0)
            {
                listBox_Lab.SelectedIndex = 0;
            }
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void splitContainer1_Panel1_Paint_1(object sender, PaintEventArgs e)
        {

        }
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "Open Dialog";
            openFileDialog.ShowDialog();
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog addFileDialog = new OpenFileDialog();
            addFileDialog.ShowDialog();
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //     OpenFileDialog removeFileFromProjectDialog = new OpenFileDialog();
            //     removeFileFromProjectDialog.ShowDialog();
        }

        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog importDataDialog = new OpenFileDialog();
            // Stream myStream = null;
            Lab tempLab = new Lab();
            tempLab.resultList = new List<Result>(100);
            tempLab.resultList.Add(new Result("k-means1"));
            //  string _LabName = null;

            importDataDialog.InitialDirectory = "D:\\";
            importDataDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            importDataDialog.FilterIndex = 2;
            importDataDialog.RestoreDirectory = true;

            if (importDataDialog.ShowDialog() == DialogResult.OK)
            {
                GlobalVariable.string1 = importDataDialog.FileName;
                CreateLabDialog lND = new CreateLabDialog();
                lND.textBox_LabName.Text = GlobalVariable.string1;
                lND.ShowDialog();
                tempLab.m_labName = GlobalVariable.string1;
                labList.Add(tempLab);
                treeView_Result.Nodes.Add("kmeans");
                treeView_Result.Nodes[0].Nodes.Add("kmeans-1");
                treeView_Result.Nodes[0].Nodes.Add("kmeans-2");
                treeView_Result.Nodes[0].Nodes.Add("kmeans-3");
                treeView_Result.Nodes[0].Nodes.Add("kmeans-4");
                treeView_Result.ExpandAll();
                //treeView_Result.Nodes.Add("kmeans3");
                //treeView_Result.Nodes.Add("kmeans4");
            }
      //      ListViewItem lVI = new ListViewItem();
     //       lVI.Text = tempLab.m_labName;
     //       listView_Lab.Items.Add(lVI);


     //       lVI.ListView.Items.Insert(1,tempLab);
     //       lVI.SubItems.Insert(1, (ListViewItem.ListViewSubItem)tempLab);
    //        lVI.ListView.Items.Add(tempLab);
   //         listView_Lab.Items.AddRange(tempLab);
  //          lVI.SubItems.Add(tempLab);
     
            //无法从“Gene_Explorer_Client.Lab”转换为“System.Windows.Forms.ListViewItem.ListViewSubItem
      //      listView_Lab.Items.Add(tempLab.m_labName);//tempLab.m_labName
            /* 
                ListViewItem lvi = new ListViewItem();
                lvi.Text = "";                
                lvi.SubItems.Add("AAA");
                lvi.SubItems.Add("BBB");
                lvi.SubItems.Add("CCC");
                listView1.Items.Add(lvi);
            */
        }

        private void exportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog exportDataDialog = new SaveFileDialog();
            exportDataDialog.ShowDialog();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveProjectDialog = new SaveFileDialog();
            saveProjectDialog.Title = "Save";
            saveProjectDialog.ShowDialog();
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveAsProjectAsDialog = new SaveFileDialog();
            saveAsProjectAsDialog.Title = "Save as";
            saveAsProjectAsDialog.ShowDialog();
        }



        



        private void prinToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GraphicPlot gd = new GraphicPlot();
            //     gd.Text = ProjectName + "PCA Plot" + graphyNumber;
            gd.Text = "PCA Plot";
            gd.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.ShowDialog();
        }




        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void toolStripButtonImport_Click(object sender, EventArgs e)
        {
            importToolStripMenuItem_Click(sender, e);

        }

        private void toolStripButtonExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog sFD = new SaveFileDialog();
            sFD.ShowDialog();
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void wizardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OrderWizardSelectDataSet oWSD = new OrderWizardSelectDataSet();
            oWSD.Show();
        }

        private void validationOrderWizard_Click(object sender, EventArgs e)
        {
            OrderWizardSelectDataSet oWSD = new OrderWizardSelectDataSet();
            oWSD.ShowDialog();
        }

        private void listViewLab_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void treeView_Result_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void treeView_Result_AfterSelect_1(object sender, TreeViewEventArgs e)
        {

        }

        private void listView_Lab_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        //private void listView_Lab_Click(object sender, EventArgs e)
        //{
        //    //textBox1.Text=this.listView1.SelectedItems[0].SubItems[1].Text;
        ////    string str = this.listView_Lab.SelectedItems.ToString();
        //    string str = GlobalVariable.string1;
        //    //string str = (string)listView_Lab.Items[0];
        //   // Console.WriteLine("中国画" + str);
        //    for (int i = 0; i < labList.Count; i++)
        //    {
        //        if (labList[i].m_labName == str)
        //        {
        //     //       GlobalVariable.result = labList[i].resultList[0];
        //            GlobalVariable.resultList = labList[i].resultList;
        //        }
        //    }
        //    TreeNode treeNode1 = new TreeNode();
        //    treeNode1.Nodes.Add(GlobalVariable.resultList[0].m_information);
        //    //索引超出范围。必须为非负值并小于集合大小。参数名: index
        //    //treeView_Result.Nodes.Add(treeNode1);
        //    //treeView_Result.Nodes.Add("kmeans1");
        //    //treeView_Result.Nodes.Add("kmeans2");
        //    //treeView_Result.Nodes.Add("kmeans3");
        //    //treeView_Result.Nodes.Add("kmeans4");
        //}

        private void dataSetManage_Click(object sender, EventArgs e)
        {
            ManageDataSet mDS = new ManageDataSet();
            mDS.Show();
        }

        private void RequiringOrder_Click(object sender, EventArgs e)
        {
            OrderInRequiring oIR = new OrderInRequiring();
            oIR.Show();
        }

        private void FinishedOrder_Click(object sender, EventArgs e)
        {
            OrderRecord oR = new OrderRecord();
            oR.Show();
        }

        private void button_Hide_Click(object sender, EventArgs e)
        {
            splitContainer1.Panel1Collapsed = true;
            panel_Show.Visible = true;
     //       if (flag == false)
     //       {
     //           //splitContainer1.Panel1Collapsed = true;
     //           splitterDistance = this.splitContainer1.SplitterDistance;
     //           buttonHide = this.button_Hide.Location;
     //           this.splitContainer1.SplitterDistance = 0;
     //           this.splitContainer1.Panel1.Padding  = new System.Windows.Forms.Padding(0, 0, 17, 0);
     //           this.splitContainer1.IsSplitterFixed = true;
     //           this.button_Hide.Image = global::Gene_Explorer_Client.Properties.Resources.right;

     //           //  this.splitContainer1.Panel1.Width = 9;
     //           //无法显式设置 SplitterPanel 的宽度。改在 SplitContainer 上设置 SplitterDistance。
     //           //        this.button_Hide.Location = new System.Drawing.Point(0, 3);
     //           //this.groupBoxDataSets.Width = 9;
     //           //   this.groupBoxResults.Location = new System.Drawing.Point(5, 66);
     //       }
     //       else
     //       {
               
     ////           this.splitContainer1.Panel1.Width = _Panel1Width;
     //           this.splitContainer1.Panel1.Padding = new System.Windows.Forms.Padding(0, 0, 0, 0);
     //           this.splitContainer1.IsSplitterFixed = false;
     //           this.splitContainer1.SplitterDistance = splitterDistance;
     //          // this.groupBoxDataSets.Width = 150;
     //           this.button_Hide.Location = buttonHide;
     //           this.button_Hide.Image = global::Gene_Explorer_Client.Properties.Resources.left;
     //        //   this.button_Hide.Location = new System.Drawing.Point(141, 3);
     //           //       this.groupBoxResults.Location = new System.Drawing.Point(150, 66);

     //       }
     //       flag = !flag;
        }

        private void button_Show_Click(object sender, EventArgs e)
        {
            splitContainer1.Panel1Collapsed = false;
            panel_Show.Visible = false;
        }



        private void CreateLabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateLabDialog lND = new CreateLabDialog(this.listBox_Lab);
            try
            {
                lND.ShowDialog();
            }
            finally
            {
                lND.Dispose();
            }
        }

        private void CancelLabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox_Lab.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择一个目录再进行删除!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("删除目录将同时删除目录下的图片,是否真的要删除“" + listBox_Lab.Text + "“?", "提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            string delFolderName = "";//用于存放被删除目录的名称
            if (dr == DialogResult.Yes)
            {
        //        lvView.Clear();
                delFolderName = listBox_Lab.Text;
                ((LabFolder)listBox_Lab.SelectedItem).RemoveAll();
                listBox_Lab.Items.Remove(listBox_Lab.SelectedItem);
                statusStrip1.Items[1].Text = "目录“" + delFolderName + "“已经被删除!";
            }
            
        }

        //自定义方法
        private void LoadToListView()
        {
            LabFolder labFolder = (LabFolder)listBox_Lab.SelectedItem;
            listView_Result.BeginUpdate();
            listView_Result.Items.Clear();
            if (!labFolder.IsLoaded)
            {
                labFolder.LoadImage();
            }
            foreach (DictionaryEntry de in labFolder.bmps)
            {
                listView_Result.Items.Add((string)de.Key);
            }
            listView_Result.EndUpdate();
        }

        private void listBox_Lab_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox_Lab.SelectedItems.Count == 0)
            {
                return;
            }
            LoadToListView();
        }

        private void toolStripButton_Toggle_Click(object sender, EventArgs e)
        {
            this.listView_Result.Visible = !this.listView_Result.Visible;
            this.treeView_Result.Visible = !this.treeView_Result.Visible;
        }

        

        

        private void listView_Result_DoubleClick(object sender, EventArgs e)
        {
            Point p = Control.MousePosition;
            p = listView_Result.PointToClient(p);
            ListViewHitTestInfo info = listView_Result.HitTest(p);
            //ShowImage();
            ShowPic sP = new ShowPic(listBox_Lab, listView_Result);
            
         //   PaintImageInPb(info.Item.Text);
            sP.PaintImageInPb(info.Item.Text);
            bmpIndex = info.Item.Index;
            sP.Show();
        }

        private void listView_Result_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            if (listView_Result.Items.Count == 0)
            {
                return;
            }
            Graphics g = e.Graphics;
            LabFolder labFolder = (LabFolder)listBox_Lab.SelectedItem;
            Bitmap bmp = labFolder.GetThumbnail(e.Item.Text);
            Rectangle bmpRect = LabFolder.GetRectFormBounds(bmp, e.Bounds);
            bmpRect.Offset(0, 1);
            Rectangle boundRect = LabFolder.GetRectFormBounds(101, 101, e.Bounds);
            Rectangle textRect = new Rectangle(e.Bounds.X + 4, e.Bounds.Y + 109, e.Bounds.Width - 8, 36);
            g.DrawRectangle(boundPen, boundRect);
            if ((e.State & ListViewItemStates.Selected) != 0)
            {
                g.DrawImage(bmp, bmpRect);//画图片的缩略图
                boundRect.Inflate(1, 1);
                g.DrawRectangle(selPen, boundRect);//画处于选中状态的边框
            }
            else
            {
                g.DrawImage(bmp, bmpRect);
            }
            format.LineAlignment = StringAlignment.Center;
            format.Alignment = StringAlignment.Center;
            g.FillRectangle(bgBrush, textRect);//填充文字背景
            g.DrawString(e.Item.Text, listView_Result.Font, textBrush, textRect, format);//绘制文字
        }

        private void DefaultToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.skinEngine1.Active = false;
        }

        private void BuleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.skinEngine1.Active = true;
            this.skinEngine1.SkinFile = "DiamondBlue.ssk";
            this.menuStripMain.Refresh();//解决菜单栏更新滞后的现象
        }

        private void GreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.skinEngine1.Active = true;
            this.skinEngine1.SkinFile = "DiamondGreen.ssk";
            this.menuStripMain.Refresh();
        }

        private void ResultViewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.listView_Result.Visible = false;
            this.treeView_Result.Visible = true;
        }

        private void PicViewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.listView_Result.Visible = true;
            this.treeView_Result.Visible = false;
        }
    }
}
